package com.ydgk.dao;

import com.ydgk.bean.Commodity;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class CommDao extends BaseDao<Commodity> {
    @Override
    public int save(Commodity commodity) {
        return this.template.update("insert into commodity VALUES (null,?,?,?,?,?,?,null)",
                commodity.getCname(),commodity.getCface(),commodity.getCprice(),commodity.getCprovider(),commodity.getCtype(),commodity.getCnum());
    }

    @Override
    public int updateById(Commodity commodity) {
        return this.template.update("update commodity set cprice=? ,cprovider=? where cid=?",
                commodity.getCprice(),commodity.getCprovider(),commodity.getCid());
    }

    @Override
    public int deleteById(int id) {
        return this.template.update("delete from commodity where cid=?",id);
    }

    @Override
    public List<Commodity> findAll() {
        return this.template.query("select * from commodity",new BeanPropertyRowMapper<Commodity>(Commodity.class));
    }

    @Override
    public Commodity findById(int id) {
        try {
             return this.template.queryForObject("select * from commodity where cid=?",new BeanPropertyRowMapper<>(Commodity.class),id);
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public List<Commodity> paging(int pageNumber) {
        return null;
    }

    public int updateCnum(int num,int cid){
        return this.template.update("update commodity set cnum=cnum-? where cid=?",num,cid);
    }

}
